// My Computer Program for PCMod (CLIENT)

PROG.PName = "Pong"
PROG.Author = "[GU]|ZLC|thomasfn"

PROG.NiceName = "Pong" -- Shown on the 2D 3D Screen

PONG = {}
PONG_Fps = 25 -- Increase to make pong run faster

function PROG:BuildWindow( WindowName, Desktop, Object )
	// Here we build the derma window.
	// Note the Desktop string is needed for every operation involving derma.
	
	local btnreset = DInterface:AddButton( "btnReset", Desktop, "Reset", 60, 25, 100, 20, ResetPong )
	local btnstart = DInterface:AddButton( "btnStart", Desktop, "Start", 160, 25, 100, 20, function()
		ResetPong()
		PONG.BSX = -0.015
		PONG.BSY = math.Rand( -0.025, 0.025 )
	end )
	
	pc_hackbutton( btnreset )
	pc_hackbutton( btnstart )

	local pnl = DInterface:MakePanel( "pnlGame", Desktop, 5, 50, Object:GetWide()-10, Object:GetTall()-55 )
	PONG = {}
	ResetPong()
	pnl.Paint = function( self )
		// Background
		draw.RoundedBox( 6, 0, 0, self:GetWide(), self:GetTall(), Color( 0, 0, 100, 255 ) )
		
		// Player Token
		surface.SetDrawColor( 255, 255, 255, 255 )
		surface.DrawRect( self:GetWide()*0.05, (self:GetTall() * (PONG.PY - 0.05) ), 10, self:GetTall() * 0.1 )
		draw.SimpleText( tostring( PONG.PS ), "PCText", 10, self:GetTall()-10, Color( 255, 255, 255, 255 ), 1, 1 )
		
		// Computer Token
		surface.SetDrawColor( 255, 255, 255, 255 )
		surface.DrawRect( self:GetWide()-(self:GetWide()*0.05)-10, (self:GetTall() * (PONG.CY - 0.05) ), 10, self:GetTall() * 0.1 )
		draw.SimpleText( tostring( PONG.CS ), "PCText", self:GetWide()-10, self:GetTall()-10, Color( 255, 255, 255, 255 ), 1, 1 )
		
		// Ball
		surface.SetDrawColor( 255, 255, 255, 255 )
		surface.DrawRect( (self:GetWide()*PONG.BX)-3, (self:GetTall()*PONG.BY)-3, 6, 6 )
		
		// Stop if we are not ready to think yet
		if (CurTimeS() < PONG.Nxt) then return end
		PONG.Nxt = CurTimeS() + (1000/PONG_Fps)
		
		// Move player
		local cx, cy = self:CursorPos()
		PONG.PY = cy / self:GetTall()
		if (PONG.PY > 1) then PONG.PY = 1 end
		if (PONG.PY < 0) then PONG.PY = 0 end
		
		// Move Ball
		PONG.BX = PONG.BX + PONG.BSX
		PONG.BY = PONG.BY + PONG.BSY
		
		// Check for collision with top wall and bottom wall
		if (PONG.BY > 1) then
			PONG.BY = 1
			PONG.BSY = -PONG.BSY
		end
		if (PONG.BY < 0) then
			PONG.BY = 0
			PONG.BSY = -PONG.BSY
		end
		
		// Check for collision with left wall and right wall
		if (PONG.BX < 0) then
			local oldps = PONG.PS
			local oldcs = PONG.CS
			ResetPong()
			PONG.PS = oldps
			PONG.CS = oldcs + 1
			timer.Create( "PongStart", 1, 1, function() PONG.BSX = 0.015; PONG.BSY = math.Rand( -0.05, 0.05 ); end )
		end
		if (PONG.BX > 1) then
			local oldps = PONG.PS
			local oldcs = PONG.CS
			ResetPong()
			PONG.PS = oldps + 1
			PONG.CS = oldcs
			timer.Create( "PongStart", 1, 1, function() PONG.BSX = -0.015; PONG.BSY = math.Rand( -0.05, 0.05 ); end )
		end
		
		// Check for collision with player token
		if ((PONG.BX < 0.05) && ((PONG.BY > (PONG.PY - 0.05)) && (PONG.BY < (PONG.PY + 0.05)))) then
			PONG.BSX = -PONG.BSX
			if (PONG.BSX > 0) then PONG.BSX = PONG.BSX + 0.0025 end
			if (PONG.BSX < 0) then PONG.BSX = PONG.BSX - 0.0025 end
		end
		
		// Check for collision with computer token
		if ((PONG.BX > 0.95) && ((PONG.BY > (PONG.CY - 0.05)) && (PONG.BY < (PONG.CY + 0.05)))) then
			PONG.BSX = -PONG.BSX
			if (PONG.BSX > 0) then PONG.BSX = PONG.BSX + 0.0025 end
			if (PONG.BSX < 0) then PONG.BSX = PONG.BSX - 0.0025 end
		end
		
		// Control AI
		if (PONG.BY > PONG.CY) then
			PONG.CY = PONG.CY + 0.02
		end
		if (PONG.BY < PONG.CY) then
			PONG.CY = PONG.CY - 0.02
		end
		if (PONG.CY > 1) then PONG.CY = 1 end
		if (PONG.CY < 0) then PONG.CY = 0 end
	end
	pnl:SetCursor( "none" )
	
	// Add a quit button
	DInterface:AddButton( "btnQuit", Desktop, "X", Object:GetWide()-20, 0, 20, 20, function() SendCommand( "quit" ); end )
end

function PROG:ClearWindow( Desktop )
	// Here we destroy EVERY derma control that MIGHT exist.
	local pnl = DInterface:GetPanel( "pnlGame" )
	if (pnl) then pnl:Remove() end
	
	DInterface:RemoveButton( "btnReset", Desktop )
	DInterface:RemoveButton( "btnStart", Desktop )
	
	DInterface:RemoveButton( "btnQuit", Desktop )
end

// Data[1] = Tag
// Data[2] = Content
function PROG:DataRecieved( Desktop, Data )
	
end

function PROG:Paint( x, y, w, h )
	// This is only for 2D 3D
	draw.RoundedBox( 6, x+(w*0.05), y+(h*0.1), w*0.9, h*0.85, Color( 0, 0, 100, 255 ) )
	surface.SetDrawColor( 255, 255, 255, 255 )
	surface.DrawRect( x+(w*0.1), y+(h*0.475), w*0.02, h*0.05 )
	surface.DrawRect( x+(w*0.9), y+(h*0.475), w*0.02, h*0.05 )
end

function ResetPong()
	PONG.PY = 0.5
	PONG.CY = 0.5
	PONG.BX = 0.5
	PONG.BY = 0.5
	PONG.BSX = 0
	PONG.BSY = 0
	PONG.PS = 0
	PONG.CS = 0
	PONG.FPS = PONG_Fps
	PONG.Nxt = CurTimeS()
end

function CurTimeS()
	return CurTime()*1000
end